package com.sky.test;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/*
@Component
@Slf4j
public class OrderTask {
    @Autowired
    OrderMapper orderMapper;

    */
/**
     * 每分钟检查未支付订单是否超时（15分钟）
     * 超时未支付则设置为取消，以及取消原因、取消时间
     *//*

    //@Scheduled(cron = "0 0/1 * * * ?")  //每分钟执行一次
    @Scheduled(cron = "0/5 * * * * ?")  //每隔5秒测试一次
    public void handleTimeOutOrder() {
        log.info("检查超时订单 {}",new Date());
        //select * from orders where status = 1 and order_time < date_sub(now(),interval 15 minute
        List<Orders> ordersList = orderMapper.getByStatusAndOrderTime(Orders.PENDING_PAYMENT, 15);
        //设置订单状态、取消原因、取消时间
        ordersList.forEach(orders -> {
            orders.setStatus(Orders.CANCELLED);
            orders.setCancelReason("订单超时未支付");
            orders.setCancelTime(LocalDateTime.now());
            orderMapper.update(orders);
            log.info("超时订单：{}，已取消",orders.getNumber());
        });
    }

    */
/**
     *  每天凌晨1点钟，处理派送超过1小时订单，设置为已完成
     *//*

    //@Scheduled(cron = "0 0 1 * * ?") //每天凌晨1点钟执行
    @Scheduled(cron = "0/10 * * * * ?")  //每隔10秒测试一次
    public void handleDeliveryOrder() {
        log.info("检查派送中订单 {}",new Date());
        //select * from orders where status = 4 and order_time < date_sub(now(),interval 60 minute
        List<Orders> ordersList = orderMapper.getByStatusAndOrderTime(Orders.DELIVERY_IN_PROGRESS, 60);
        //设置订单状态、取消原因、取消时间
        ordersList.forEach(orders -> {
            orders.setStatus(Orders.COMPLETED);
            orderMapper.update(orders);
            log.info("订单派送超过1小时未确认：{}，已自动确认",orders.getNumber());
        });
    }
}
*/

